package com.part2.action.function;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;

import com.part2.Constants;
import com.part2.action.common.CommonAction;
import com.part2.action.common.RandomGUID;
import com.part2.action.common.form.CustomActionForm;
import com.part2.action.common.form.ValueObject;
import com.part2.action.common.validator.Validator;
import com.part2.beans.Bigclass;
import com.part2.beans.Smallclass;
import com.part2.business.function.BigClassService;
import com.part2.business.function.ProductsService;
import com.part2.common.query.Page;

public class BigClassAction extends CommonAction<Bigclass> {
	private final Log logger = LogFactory.getLog(getClass());
	private BigClassService bigClassService;
	private ProductsService productsService;

	public void setBigClassService(BigClassService bigClassService) {
		this.bigClassService = bigClassService;
	}

	public void setProductsService(ProductsService productsService) {
		this.productsService = productsService;
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("::::::::::BigClass-List");
		Page<Bigclass> p = initPage(request, Constants.PRE_PAGE_SIZE);// 设置每页的记录数
		Page<Bigclass> page = bigClassService.findBigClass(p);
		if (page != null) {
			request.setAttribute("page", page);

		} else {
			request.setAttribute("res", "暂无记录！");
		}
		logger.info("::::::::::BigClass-List End");
		return mapping.findForward("bigClassList");
	}

	public ActionForward addBigClass(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("::::::::::BigClass-Add");
		ValueObject bVo = ((CustomActionForm) form).getVoa().getValue("big");
		String name = bVo.getField("name");
		String nameEn = bVo.getField("nameEn");
		String order = bVo.getField("order");
		String id = new RandomGUID() + "-bClass";
		Bigclass bClass = new Bigclass();
		bClass.setBclassId(id);
		bClass.setBclassName(name);
		bClass.setBclassNameEn(nameEn);
		bClass.setOrderB(Short.parseShort(order));
		bigClassService.addBigClass(bClass);
		logger.info("::::::::::BigClass-Add End");
		return list(mapping, form, request, response);
	}

	public ActionForward editBigClass(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("::::::::::BigClass-Edit");
		ValueObject bVo = ((CustomActionForm) form).getVoa().getValue("edit");
		String bclassId = bVo.getField("id");
		String name = bVo.getField("name");
		String nameEn = bVo.getField("nameEn");
		String order = bVo.getField("order");
		if (Validator.isNotNull(bclassId)) {
			Bigclass bc = bigClassService.findBigClassByPrimeKey(bclassId);
			if (bc != null) {
				bc.setBclassName(name);
				bc.setBclassNameEn(nameEn);
				if (Validator.isNotNull(order))
					bc.setOrderB(Short.parseShort(order));
				bigClassService.updateBigClass(bc);
			}
		}
		logger.info("::::::::::BigClass-Edit End");
		return list(mapping, form, request, response);
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("::::::::::BigClass-Delete");
		String id = request.getParameter("bclassId");
		ActionMessages errors = new ActionMessages();
		if (Validator.isNotNull(id)) {
			List<Smallclass> s = bigClassService.findSmallClassByBigClass(id);
			if (s != null && s.size() > 0) {
				response.setContentType("text/html;charset=UTF-8"); 
				response
						.getWriter()
						.write(
								"<script language='javascript'>alert('"+"删除失败!请将该类下小类删除后再删除!"+"');history.go(-1); </script>");
				return null;
			}else{
				bigClassService.deleteBigClass(id);
				logger.info("::::::::::delete " + id + " success");
				return this.list(mapping, form, request, response);
				}
		}
		return this.list(mapping, form, request, response);
	}
}
